<?php

namespace App\Http\Controllers\Admin;

use App\Models\Member;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MemberController extends Controller
{
    //
    public function index()
    {

        return view('admin.member.index');
    }

    public function indexPost(Request $request)
    {
        $input = $request->all();
        $condition = [
            'condition' => []
        ];
        $serach = [];
        if($request->has('serach_name')){
            //dd($request->input('serach_name'));
            $condition = [
                'condition' => [
                    [
                        'where',
                        ['where','name like %?%']
                    ]
                ]
            ];
            $serach[0] = $request->input('serach_name');
        }
        if($request->has('time_start')){
            if($request->has('time_end')){
                $condition['condition'][] = [
                    'where',
                    ['where', 'created_at >= ?'],
                    ['where', 'created_at <= ?']
                ];
                $serach[] = $request->input('time_start');
            }else{
                $condition['condition'][] = [
                    'where',
                    ['where', 'created_at >= ?'],
                ];
                $serach[] = $request->input('time_start');
                $serach[] = $request->input('time_end');
            }
        }
        if($request->has('search_status')){
            $condition['condition'][] = [
                'where',
                ['where', 'status = ?'],
            ];
            $serach[] = $request->input('search_status');
        }
        $dd = (new Member())->dataTable10(['*'], $input ,$condition, $serach);
        return response()->json($dd);
    }

    /**
     *  添加用户
     */
    public function addMember()
    {

        return view('admin.member.addMember');
    }

    public function addMemberPost(Request $request)
    {
        $input = [
            'name' => $request->input('name'),
            'mobile' => $request->input('mobile'),
            'openid' => $request->input('openid'),
            'password' => $request->input('password','123456'),
            'lev_id' => $request->input('lev_id'),
            'status' => $request->input('status'),
            'sex' => $request->input('sex')
        ];
        if($request->has('id')){
            $res = Member::find($request->input('id'))->update($input);
        }else{
            $res = Member::create($input);
        }
        if($res){
            return response()->json(['code'=>1, 'msg'=>'成功']);
        }else{
            return response()->json(['code'=>0, 'msg'=>'失败']);
        }

    }

    /**
     *  编辑用户
     */
    public function editMember($id = 0)
    {
        $member_info = Member::find($id);

        return view('admin.member.addMember',['id'=>$id, 'member_info'=>$member_info]);
    }

    /**
     *  删除用户
     */
    public function delMember(Request $request)
    {
        if($request->has('id')){
            $id = $request->input('id');
            $res = Member::find($id)->delete();
            if($res){
                return response()->json();
            }else{
                return response()->json();
            }
        }
    }


    /**
     *
     *  用户通过审核
     */
    public function accessMember(Request $request)
    {
        if($request->has('id')){
            $input = [
                'status' => 1,
            ];
            $res = Member::find($request->input('id'))->update($input);
            if($res){
                return response()->json(['code'=>1, 'msg'=>'成功']);
            }else{
                return response()->json(['code'=>0, 'msg'=>'失败']);
            }
        }
    }


}
